import pyglet
from pyglet.gl import glColor3f
from pyglet.gl import glBegin
from pyglet.gl import glEnd
from pyglet.gl import GL_QUADS
from pyglet.gl import glLoadIdentity
from pyglet.gl import glVertex2f
from rect import Rect

class Frame(Rect):

    def _draw_routine(self):
        glColor3f(224/255.0, 223/255.0, 222/255.0)
        glLoadIdentity()

        glBegin(GL_QUADS)
        glVertex2f(self.left, self.bottom)
        glVertex2f(self.left, self.top)
        glVertex2f(self.right, self.top)
        glVertex2f(self.right, self.bottom)
        glEnd()


    def close(self):
        self._window.close()


    def __init__(self, children=[], *args, **kwargs):
        self._window = pyglet.window.Window()
        Rect.__init__(self, 0, 0, self._window.width, self._window.height)
        
        self.children = [children]
        for child in self.children[0]:
            child._register_frame(self)


        @self._window.event
        def on_draw():
            self._window.clear()
            self._draw_routine()

            for lvl in self.children:
                for child in lvl:
                    child.draw()


        @self._window.event
        def on_key_press(symbol, modifiers):
            for lvl in self.children:
                for child in lvl:
                    child.on_key_press(symbol, modifiers)


        @self._window.event
        def on_key_release(symbol, modifiers):
            for lvl in self.children:
                for child in lvl:
                    child.on_key_release(symbol, modifiers)


        @self._window.event
        def on_mouse_press(x, y, button, modifier):
            for lvl in self.children[::-1]:
                for child in lvl:
                    
                    if child.on_click(x, y, button, modifier):
                        return


        @self._window.event
        def on_mouse_release(x, y, button, modifier):
            for lvl in self.children:
                for child in lvl:
                    child.on_release(x, y, button, modifier)


        @self._window.event
        def on_mouse_motion(x, y, dx, dy):
            for lvl in self.children[::-1]:
                for child in lvl:
                    
                    if child.on_motion(x, y, dx, dy):
                        return

        
        @self._window.event
        def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
            for lvl in self.children[::-1]:
                for child in lvl:
                    
                    if child.on_drag(x, y, dx, dy, buttons, modifiers):
                        return


        @self._window.event
        def on_mouse_scroll(x, y, scroll_x, scroll_y):
            for lvl in self.children[::-1]:
                for child in lvl:

                    if child.on_scroll(x, y, scroll_x, scroll_y):
                        return


    def add_child(self, lvl, child):
        if len(self.children) <= lvl:
            self.children.append([])
        self.children[lvl].append(child)
        child._register_frame(self)

    def run(self):
        pyglet.app.run()
